package com.jdbctest.training.exer;

import com.jdbctest.numbertwo.JDBCUtils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;

//练习1:操作学生信息
public class Exer1 {

    @Test
    public void testInsert() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入用户名：");
        String name = scanner.next();
        System.out.print("请输入邮箱：");
        String email = scanner.next();
        System.out.print("请输入生日：");
        String birthday = scanner.next();//'1992-09-08'

        String sql = "insert into customers(name,email,birth)values(?,?,?)";
        int insertCount = update(sql,name,email,birthday);
        if(insertCount > 0){
            System.out.println("添加成功");

        }else{
            System.out.println("添加失败");
        }
    }

    //通用的增删改操作
    public int update(String sql, Object... args) {  //sql中占位符的个数与可变形参的长度相同
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1.获取数据库连接
            connection = JDBCUtils.getConnection();
            //2.预编译sql语句，返回PreparedStatement的实例
            preparedStatement = connection.prepareStatement(sql);
            //3.填充占位符
            for (int i = 0; i < args.length; i++) {
                //小心参数声明错误!!
                preparedStatement.setObject(i + 1, args[i]);
            }

            //4.执行
            /**
             * ps.execute();
             * 如果执行的是查询操作，有返回结果,则此方法返回true
             * 如果执行的是增，删，改操作，没有返回结果，则此方法返回false
             */
            //方式一:
//            return preparedStatement.execute();
            //方式二:
            return preparedStatement.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, preparedStatement);
        }

        return 0;
    }
}
